package puzzle.platformer.game;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import puzzle.platformer.game.puzzleActivity.Panel;
import android.content.Context;
import android.content.res.AssetManager;

public class LevelFileReader{
	private Context context;
	private Panel panel;
	
	public LevelFileReader(Context context,Panel panel){
		this.context = context;
		this.panel = panel;
	}
	
	public void readFileLevel(String filename) throws IOException{
		AssetManager am = context.getAssets();
		InputStream is = am.open(filename);
		DataInputStream in = new DataInputStream(is);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		//remove everything from the level, so new level can be loaded.
		panel.clearLevel();
		while ((strLine = br.readLine()) != null){
			int x = 0;
			int y = 0;
			int imageid = 0;
			int width = 0;
			int height = 0;
			if(strLine.contains("playr:")){
				x = Integer.parseInt(strLine.substring(7,10));
				y = Integer.parseInt(strLine.substring(11,14));
				imageid = Integer.parseInt(strLine.substring(15,25));
				width = Integer.parseInt(strLine.substring(26,29));
				height = Integer.parseInt(strLine.substring(30));
				panel.setPlayer(x,y,imageid,width, height);
			}
			else if(strLine.contains("block:")){
				x = Integer.parseInt(strLine.substring(7,10));
				y = Integer.parseInt(strLine.substring(11,14));
				imageid = Integer.parseInt(strLine.substring(15,25));
				width = Integer.parseInt(strLine.substring(26));
				panel.addBlock(x,y,imageid,width);
			}
		}
	}
}